package com.jackrain.nea.fc.pos.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.pos.api.ViewDLRetailToF4QueryCmd;
import com.jackrain.nea.fc.pos.mapper.ViewDLRetailToF4Mapper;
import com.jackrain.nea.fc.utils.Constants;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author wuwenxue
 * @date 2018/05/22
 **/
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "pos")
public class ViewDLRetailToF4QueryCmdImpl extends CommandAdapter implements ViewDLRetailToF4QueryCmd {

    private final static FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd");

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        ValueHolder vh = new ValueHolder();
        ViewDLRetailToF4Mapper viewDLRetailToF4Mapper = ApplicationContextHandle.getBean(
                ViewDLRetailToF4Mapper.class);
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(
                map.get("param"), "yyyy-MM-dd HH:mm:ss"), Feature.OrderedField);
        log.info("ViewDLRetailToF4QueryCmdImpl-->param-->{}",param);

        if (null == param || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }

        Object[] ids = param.getJSONArray("IDS").toArray();
        String action = param.getString("action");
        JSONArray jsonArray = new JSONArray();
        if ("account".equals(action)) {
            //记账
            List<HashMap> list = viewDLRetailToF4Mapper.selRetailData(ids, 2, null);
            for (HashMap hashMap : list) {
                Long id = TypeUtils.castToLong(hashMap.get("ID"));

                Date billDate = TypeUtils.castToDate(hashMap.get("BILL_DATE"));
                String formatBillDate = DATE_FORMAT.format(billDate);

                hashMap.put("BILL_DATE", formatBillDate);

                List<HashMap> itemList = viewDLRetailToF4Mapper.selRetailItemData(id);
                JSONArray itemArray = new JSONArray();
                for (HashMap itemH : itemList) {

                    String saleType = TypeUtils.castToString(itemH.get("SALE_TYPE"));
                    if (null != saleType) {
                        itemH.put("RETAIL_TYPE", saleType);
                    } else {
                        itemH.put("RETAIL_TYPE", null);
                    }

                    JSONObject itemjs = new JSONObject(itemH);
                    itemArray.add(itemjs);
                }

                List<HashMap> itempayList = viewDLRetailToF4Mapper.selRetailPayItemData(id);
                JSONArray itempayArray = new JSONArray();
                for (HashMap itempayH : itempayList) {
                    JSONObject itempayjs = new JSONObject(itempayH);
                    itempayArray.add(itempayjs);
                }

                JSONObject js = new JSONObject(hashMap);
                js.put("DL_B_RETAIL_ITEM", itemArray);
                js.put("DL_B_RETAIL_PAY_ITEM", itempayArray);
                log.debug("ViewDLRetailToF4QueryCmdImpl--account-->{}",js);

                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(js,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);

            }
        } else if ("unaccount".equals(action)) {

            //取消记账
            List<HashMap> list = viewDLRetailToF4Mapper.selRetailData(ids, 3, null);

            for (HashMap hashMap : list) {
                JSONObject terJson = new JSONObject();
                terJson.put("BILL_TYPE", "RETAIL");
                terJson.put("BILL_NO", hashMap.get("BILL_NO"));
                terJson.put("STATUS_TYPE", "UNCHARG");
                terJson.put("OPR_ECODE", hashMap.get("MODIFIERNAME"));
                terJson.put("OPR_ENAME", hashMap.get("MODIFIERENAME"));
                terJson.put("OPR_DATE", hashMap.get("MODIFIEDDATE"));
                log.debug("ViewDLRetailToF4QueryCmdImpl--unaccount-->{}",terJson);
                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(terJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }

        } else if ("void".equals(action)) {

            //作废
            List<HashMap> list = viewDLRetailToF4Mapper.selRetailData(ids, null, "N");

            for (HashMap hashMap : list) {
                JSONObject terJson = new JSONObject();
                terJson.put("BILL_TYPE", "RETAIL");
                terJson.put("BILL_NO", hashMap.get("BILL_NO"));
                terJson.put("STATUS_TYPE", "DEL");
                terJson.put("OPR_ECODE", hashMap.get("MODIFIERNAME"));
                terJson.put("OPR_ENAME", hashMap.get("MODIFIERENAME"));
                terJson.put("OPR_DATE", hashMap.get("MODIFIEDDATE"));
                log.debug("ViewDLRetailToF4QueryCmdImpl--void-->{}",terJson);
                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(terJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }

        } else if ("addvip".equals(action)) {

            //补录卡号
            List<HashMap> list = viewDLRetailToF4Mapper.selRetailData(ids, null, "Y");

            for (HashMap hashMap : list) {
                JSONObject terJson = new JSONObject();
                terJson.put("BILL_TYPE", "RETAIL");
                terJson.put("BILL_NO", hashMap.get("BILL_NO"));
                terJson.put("STATUS_TYPE", "ADD_VIP");
                terJson.put("USERDEFINE1", hashMap.get("VP_C_VIP_ECODE"));
                terJson.put("OPR_ECODE", hashMap.get("MODIFIERNAME"));
                terJson.put("OPR_ENAME", hashMap.get("MODIFIERENAME"));
                terJson.put("OPR_DATE", hashMap.get("MODIFIEDDATE"));
                terJson.put("USERDEFINE2", hashMap.get("VIP_SCORE"));
                log.debug("ViewDLRetailToF4QueryCmdImpl--addvip-->{}",terJson);
                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(terJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }

        }


        vh.put("data", jsonArray);
        return vh;
    }


}
